package com.ds.config;

import org.hibernate.validator.internal.engine.MessageInterpolatorContext;

import javax.validation.MessageInterpolator;
import java.util.Locale;

/**
 * @class: Varitor
 * @description:
 * @author: fs
 * @create: 2024/4/8 17:49
 **/

public class CustomMessageInterpolator implements MessageInterpolator {
    private final MessageInterpolator delegate;

    public CustomMessageInterpolator(MessageInterpolator delegate) {
        this.delegate = delegate;
    }

    @Override
    public String interpolate(String messageTemplate, Context context) {

        return delegate.interpolate(messageTemplate, context);
    }

    @Override
    public String interpolate(String messageTemplate, Context context, Locale locale) {
        if (context instanceof MessageInterpolatorContext interpolatorContext) {
            String filedName = interpolatorContext.getPropertyPath().toString();
            return filedName + " " + delegate.interpolate(messageTemplate, context, locale);
        }
        return delegate.interpolate(messageTemplate, context, locale);
    }
}
